<?php


namespace app\api\logic;


use app\api\model\Code as CodeModel;

class Common
{

    public static function checkCode($data,$type,$role){
        $where['type']=$type;
        $where['is_use']=0;
        $where['role']=$role;
        if(isset($data['phone'])){
            $where['phone']=$data['phone'];
            $where['phone_code']=$data['phone_code'];
        }else{
            $where['email']=$data['email'];
        }
        $code=CodeModel::where($where)->where('expire_time','>',time())->field('id,code')->find();
        if(empty($code)){
            $result['req']=0;
            $result['msg']='The verification code does not exist or has expired';
            return $result;
        }
        if($code['code']!=$data['code']){
            $result['req']=0;
            $result['msg']='Verification code error';
            return $result;
        }
        //code改为已使用
        $result['req']=1;
        CodeModel::where(['id'=>$code['id']])->update(['is_use'=>1]);
        return $result;
    }
}